﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Portal.Utility;
using Portal.Xml.Utility;

namespace Portal.Cms.Model
{
    public class DataSaveHistoryCls
    {
        public string DataSaveHistoryId;
        public string DataId;
        public string WorkflowDefId;
        public string DataAlias;
        public string GroupDataAlias;
        public string GroupDataName;
        public string frkGroupDataId;
        public string ParentDataId;
        public string Subject;
        public string OtherSubject;
        public string Description;
        public string Body;
        public string Source;
        public DateTime EntryDate;
        public DateTime UpdatedDate;
        public string Avatar;
        public string Url;
        public string Target;
        public string EntryBy;
        public string AssetCode;
        public string DataLanguage;
        public string DataType;

        public string frkDocTypeId;
        public string DocTypeName;

        public string frkDocReleaseFromId;
        public string DocReleaseFromName;

        public string frkDocSignerId;
        public string DocSignerName;

        public string frkDocFieldId;
        public string DocFieldName;

        public string frkDocYearRangeId;
        public string DocYearRangeName;

        public string EffectedDate;
        public string EndEffectedDate;
        public string ExpiredDate;
        public string SignDate;
        public string ReleaseDate;
        public string ReplaceDoc;
        public string ModifyDoc;
        public string DocStatus;
        public string AttachDoc = "";
        public string VoteType = "";

        public int IsPublic;
        public int SortIndex;
        public int TotalPreview = 0;
        public int TotalDownload = 0;
        public int IsNew = 0;
        public int AttachedSearchable = 0;
        public int TotalVote = 0;
        public string VoteDataText = "";
        public string SubDataType = "";
        public string RelationTypeName = "";
        public int NeedApproved = 0;

        //cho bao.....
        public string CategoryIds = "";
        public string HotCategoryIds = "";

        public string MapCategoryIds = "";
        public string MapHotCategoryIds = "";


        public string NewsTypeId = "";
        public string ImageLevelId = "";
        public string EventId = "";
        public int ExpImageCount = 0;
        public string ExpImageLevelId = "";
        public string NewsLevelId = "";
        public int IsFocus = 0;
        public int IsShowAuthor = 0;
        public int IsAdv = 0;
        public string Keyword = "";
        public string SurveyCode = "";
        public string Note = "";
        public string Categories = "";

        public DateTime SaveDate;
        public string SaveById;
        public string SaveByFullName;

        public string SaveRoleId;
        public string SaveRoleName;

        public string GetAttachDocUrl(string SiteId, string HttpDataRoot)
        {
            //Environments.HttpDataRoot(SiteId) 
            return HttpDataRoot + "/DocumentArchives/" + AssetCode + "/ArchiveDoc/Archive/" + AttachDoc;
        }

    }

    public class DataSaveHistoryParser
    {
        public static DataSaveHistoryCls CreateInstance()
        {
            //tao 1 class trang....
            DataSaveHistoryCls oDataSaveHistoryCls = new DataSaveHistoryCls();
            return oDataSaveHistoryCls;
        }

        public static DataSaveHistoryCls ParseFromXml(string XmlData)
        {
            DataSet ds = XmlUtility.GetDataSetFromXml(XmlData);
            DataSaveHistoryCls oDataSaveHistoryCls = ParseFromDataRow(ds.Tables[0].Rows[0]);
            return oDataSaveHistoryCls;
        }


        public static DataSaveHistoryCls ParseFromDataRow(DataRow dr)
        {
            DataSaveHistoryCls oDataSaveHistoryCls = new DataSaveHistoryCls();
            oDataSaveHistoryCls.DataId = XmlUtility.GetString(dr, "DataId", true);
            oDataSaveHistoryCls.DataSaveHistoryId = XmlUtility.GetString(dr, "DataSaveHistoryId", true);
            oDataSaveHistoryCls.DataAlias = XmlUtility.GetString(dr, "DataAlias", true);
            oDataSaveHistoryCls.GroupDataAlias = XmlUtility.GetString(dr, "GroupDataAlias", true);
            oDataSaveHistoryCls.GroupDataName = XmlUtility.GetString(dr, "GroupDataName", true);
            oDataSaveHistoryCls.Subject = XmlUtility.GetString(dr, "Subject", true);
            oDataSaveHistoryCls.OtherSubject = XmlUtility.GetString(dr, "OtherSubject", true);
            oDataSaveHistoryCls.Description = XmlUtility.GetString(dr, "Description", true);
            oDataSaveHistoryCls.Body = XmlUtility.GetString(dr, "Body", true);
            oDataSaveHistoryCls.Source = XmlUtility.GetString(dr, "Source", true);
            oDataSaveHistoryCls.EntryDate = XmlUtility.GetDate(dr, "EntryDate", true);
            oDataSaveHistoryCls.UpdatedDate = XmlUtility.GetDate(dr, "UpdatedDate", true);
            oDataSaveHistoryCls.Avatar = XmlUtility.GetString(dr, "Avatar", true);
            oDataSaveHistoryCls.Url = XmlUtility.GetString(dr, "Url", true);
            oDataSaveHistoryCls.Target = XmlUtility.GetString(dr, "Target", true);
            oDataSaveHistoryCls.EntryBy = XmlUtility.GetString(dr, "EntryBy", true);
            oDataSaveHistoryCls.IsPublic = XmlUtility.GetInt(dr, "IsPublic", true);
            oDataSaveHistoryCls.SortIndex = XmlUtility.GetInt(dr, "SortIndex", true);
            oDataSaveHistoryCls.frkGroupDataId = XmlUtility.GetString(dr, "frkGroupDataId", true);
            oDataSaveHistoryCls.AssetCode = XmlUtility.GetString(dr, "AssetCode", true);
            oDataSaveHistoryCls.DataType = XmlUtility.GetString(dr, "DataType", true);
            oDataSaveHistoryCls.AttachDoc = XmlUtility.GetString(dr, "AttachDoc", true);
            oDataSaveHistoryCls.VoteType = XmlUtility.GetString(dr, "VoteType", true);
            
            oDataSaveHistoryCls.frkDocTypeId = XmlUtility.GetString(dr, "frkDocTypeId", true);
            oDataSaveHistoryCls.DocTypeName = XmlUtility.GetString(dr, "DocTypeName", true);

            oDataSaveHistoryCls.frkDocFieldId = XmlUtility.GetString(dr, "frkDocFieldId", true);
            oDataSaveHistoryCls.DocFieldName = XmlUtility.GetString(dr, "DocFieldName", true);

            oDataSaveHistoryCls.frkDocYearRangeId = XmlUtility.GetString(dr, "frkDocYearRangeId", true);
            oDataSaveHistoryCls.DocYearRangeName = XmlUtility.GetString(dr, "DocYearRangeName", true);
            
         
            oDataSaveHistoryCls.frkDocReleaseFromId = XmlUtility.GetString(dr, "frkDocReleaseFromId", true);
            oDataSaveHistoryCls.DocReleaseFromName = XmlUtility.GetString(dr, "DocReleaseFromName", true);
            oDataSaveHistoryCls.frkDocSignerId = XmlUtility.GetString(dr, "frkDocSignerId", true);
            oDataSaveHistoryCls.DocSignerName = XmlUtility.GetString(dr, "DocSignerName", true);

            oDataSaveHistoryCls.VoteType = XmlUtility.GetString(dr, "VoteType", true);

            oDataSaveHistoryCls.WorkflowDefId = XmlUtility.GetString(dr, "WorkflowDefId", true);
            

            oDataSaveHistoryCls.ParentDataId = XmlUtility.GetString(dr, "ParentDataId", true);
            oDataSaveHistoryCls.DataLanguage = XmlUtility.GetString(dr, "DataLanguage", true);
            oDataSaveHistoryCls.SubDataType = XmlUtility.GetString(dr, "SubDataType", true);
            oDataSaveHistoryCls.RelationTypeName = XmlUtility.GetString(dr, "RelationTypeName", true);
            try
            {
                oDataSaveHistoryCls.EffectedDate = XmlUtility.GetDate(dr, "EffectedDate", true).ToString("dd/MM/yyyy");
                if (oDataSaveHistoryCls.EffectedDate.Equals("01/01/1900"))
                {
                    oDataSaveHistoryCls.EffectedDate = "";
                }
            }
            catch { }


            try
            {
                oDataSaveHistoryCls.EndEffectedDate = XmlUtility.GetDate(dr, "EndEffectedDate", true).ToString("dd/MM/yyyy");
                if (oDataSaveHistoryCls.EndEffectedDate.Equals("01/01/1900"))
                {
                    oDataSaveHistoryCls.EndEffectedDate = "";
                }
            }
            catch { }

            try
            {
                oDataSaveHistoryCls.SignDate = XmlUtility.GetDate(dr, "SignDate", true).ToString("dd/MM/yyyy");
                if (oDataSaveHistoryCls.SignDate.Equals("01/01/1900"))
                {
                    oDataSaveHistoryCls.SignDate = "";
                }
            }
            catch { }
            try
            {
                oDataSaveHistoryCls.ReleaseDate = XmlUtility.GetDate(dr, "ReleaseDate", true).ToString("dd/MM/yyyy");
                if (oDataSaveHistoryCls.ReleaseDate.Equals("01/01/1900"))
                {
                    oDataSaveHistoryCls.ReleaseDate = "";
                }
            }
            catch { }
            oDataSaveHistoryCls.ReplaceDoc = XmlUtility.GetString(dr, "ReplaceDoc", true);
            oDataSaveHistoryCls.ModifyDoc = XmlUtility.GetString(dr, "ModifyDoc", true);
            oDataSaveHistoryCls.DocStatus = XmlUtility.GetString(dr, "DocStatus", true);

            oDataSaveHistoryCls.TotalPreview = XmlUtility.GetInt(dr, "TotalPreview", true);
            oDataSaveHistoryCls.TotalDownload = XmlUtility.GetInt(dr, "TotalDownload", true);
            oDataSaveHistoryCls.IsNew = XmlUtility.GetInt(dr, "IsNew", true);
            oDataSaveHistoryCls.AttachedSearchable = XmlUtility.GetInt(dr, "AttachedSearchable", true);
            oDataSaveHistoryCls.TotalVote = XmlUtility.GetInt(dr, "TotalVoteChecked", true);
            oDataSaveHistoryCls.VoteDataText = XmlUtility.GetString(dr, "VoteDataText", true);
            oDataSaveHistoryCls.SubDataType = XmlUtility.GetString(dr, "SubDataType", true);
            oDataSaveHistoryCls.NeedApproved = XmlUtility.GetInt(dr, "NeedApproved", true);

            //danh cho báo
            oDataSaveHistoryCls.CategoryIds = XmlUtility.GetString(dr, "CategoryIds", true);
            oDataSaveHistoryCls.HotCategoryIds = XmlUtility.GetString(dr, "HotCategoryIds", true);
            oDataSaveHistoryCls.NewsTypeId = XmlUtility.GetString(dr, "NewsTypeId", true);
            oDataSaveHistoryCls.ImageLevelId = XmlUtility.GetString(dr, "ImageLevelId", true);
            oDataSaveHistoryCls.EventId = XmlUtility.GetString(dr, "EventId", true);
            oDataSaveHistoryCls.ExpImageCount = XmlUtility.GetInt(dr, "ExpImageCount", true);
            oDataSaveHistoryCls.ExpImageLevelId = XmlUtility.GetString(dr, "ExpImageLevelId", true);
            oDataSaveHistoryCls.NewsLevelId = XmlUtility.GetString(dr, "NewsLevelId", true);
            oDataSaveHistoryCls.IsFocus = XmlUtility.GetInt(dr, "IsFocus", true);
            oDataSaveHistoryCls.IsShowAuthor = XmlUtility.GetInt(dr, "IsShowAuthor", true);
            oDataSaveHistoryCls.IsAdv = XmlUtility.GetInt(dr, "IsAdv", true);
            oDataSaveHistoryCls.Keyword = XmlUtility.GetString(dr, "Keyword", true);
            oDataSaveHistoryCls.SurveyCode = XmlUtility.GetString(dr, "SurveyCode", true);
            oDataSaveHistoryCls.Note = XmlUtility.GetString(dr, "Note", true);
            oDataSaveHistoryCls.Categories = XmlUtility.GetString(dr, "Categories", true);

            oDataSaveHistoryCls.SaveById = XmlUtility.GetString(dr, "SaveById", true);
            oDataSaveHistoryCls.SaveByFullName = XmlUtility.GetString(dr, "SaveByFullName", true);

            oDataSaveHistoryCls.SaveRoleId = XmlUtility.GetString(dr, "SaveRoleId", true);
            oDataSaveHistoryCls.SaveRoleName = XmlUtility.GetString(dr, "SaveRoleName", true);
            oDataSaveHistoryCls.SaveDate = XmlUtility.GetDate(dr, "SaveDate", true);
            
            try
            {
                oDataSaveHistoryCls.ExpiredDate = XmlUtility.GetDate(dr, "ExpiredDate", true).ToString("dd/MM/yyyy");
                //if (oDataSaveHistoryCls.ExpiredDate.Equals("01/01/0001"))
                if (oDataSaveHistoryCls.ExpiredDate.Equals("01/01/1900"))
                {
                    oDataSaveHistoryCls.ExpiredDate = "";
                }
            }
            catch { }
            return oDataSaveHistoryCls;
        }

        public  static DataSaveHistoryCls[] ParseFromDataTable(DataTable dtTable)
        {
            DataSaveHistoryCls[] DataSaveHistorys = new DataSaveHistoryCls[dtTable.Rows.Count];
            for (int iIndex = 0; iIndex < dtTable.Rows.Count; iIndex++)
            {
                DataSaveHistorys[iIndex] = ParseFromDataRow(dtTable.Rows[iIndex]);
            }

            return DataSaveHistorys;
        }

        public static DataSaveHistoryCls[] ParseMultiRecordFromXml(string XmlData)
        {
            DataSet ds = XmlUtility.GetDataSetFromXml(XmlData);
            if (ds.Tables.Count == 0)
            {
                return new DataSaveHistoryCls[0];
            }
            DataSaveHistoryCls[] DataSaveHistorys = new DataSaveHistoryCls[ds.Tables[0].Rows.Count];
            for (int iIndex = 0; iIndex < ds.Tables[0].Rows.Count; iIndex++)
            {
                DataSaveHistorys[iIndex] = ParseFromDataRow(ds.Tables[0].Rows[iIndex]);
            }
            return DataSaveHistorys;
        }
    }
}
